/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *
 */
package org.cholm.media.castdr.backend;

import org.json.JSONObject;

/**
 * A particular broadcast
 *
 * @author cholm
 */
public class Program {

    /**
     * Index of low resolution URL
     */
    public static final int LOW = 0;
    /**
     * Index of medium resolution URL
     */
    public static final int MEDIUM = 1;
    /**
     * Index of high resolution url
     */
    public static final int HIGH = 2;
    /**
     * Video ID
     */
    protected String mId = null;
    /**
     * Title
     */
    protected String mTitle = null;
    /**
     * When broadcasted
     */
    protected String mDate = null;
    /**
     * streams
     */
    protected String[] mUrls = {null, null, null};
    /**
     * Back-link
     */
    protected Serie mSerie = null;

    /**
     * Create a video object from a JSON object
     *
     * @param json JSON object to decode
     */
    public Program(JSONObject json) {
        mId = json.optString("id");
        mTitle = json.optString("title");
        mDate = json.optString("formattedBroadcastTime");
    }

    /**
     * Check if this is valid
     *
     * @param testStreams if true, also test links
     * @return true if valid
     */
    public boolean isValid(boolean testStreams) {
        if (mId == null || mId.isEmpty()) return false;
        if (testStreams && !hasLink()) return false;
        return true;
    }

    /**
     * Check if any link is set
     *
     * @return true if at least one link is set
     */
    public boolean hasLink() {
        if (mUrls[LOW] == null && mUrls[MEDIUM] == null && mUrls[HIGH] == null)
            return false;
        return true;
    }

    /**
     * Massage link to media over HTTP
     *
     * @param url Input RTSP url
     * @return HTTP url
     */
    protected String makeLink(String url) {
        return "http://vodfiles.dr.dk/"
                + url.substring(url.indexOf("CMS/Resources/"), url.length());
    }

    /**
     * Set the links
     *
     * @param streams JSON object with information
     */
    public void setLinks(JSONObject streams) {
        mUrls[Program.LOW] = makeLink(streams.optString("low"));
        mUrls[Program.MEDIUM] = makeLink(streams.optString("medium"));
        mUrls[Program.HIGH] = makeLink(streams.optString("high"));
    }

    /**
     * @return the id
     */
    public String getId() {
        return mId;
    }

    /**
     * @return the slug
     */
    public String getTitle() {
        return mTitle;
    }

    /**
     * @return the desc
     */
    public String getDate() {
        return mDate;
    }

    /**
     * @return the mani
     */
    public String getLink(int qual) {
        return mUrls[qual];
    }

    public Serie getSerie() {
        return mSerie;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return mTitle;
    }
}

/* EOF */
